package com.xywl.escort.view.coupon;

import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.CouponDetailResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

/**
 * Created by 32422 on 2018/3/7.
 */

public class CouponDetailPresenter implements CouponDetailContract.Presenter {
    private CouponDetailContract.View mView;
    private ApiServer mApiServer;

    @Inject
    CouponDetailPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        this.mView = null;
    }

    @Override
    public void takeView(CouponDetailContract.View view) {
        this.mView = view;
    }

    @Override
    public void redPacketDetail(String id) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.redPacketDetail(req)
                .compose(RxUtil.<CouponDetailResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<CouponDetailResp>bindToLife())
                .subscribe(new BaseSubscriber<CouponDetailResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.redPacketDetailFailure("获取红包详情失败，请稍后重试");
                    }

                    @Override
                    public void onNext(CouponDetailResp resp) {
                        if (null != resp) {
                            if (resp.isOk() && null != resp.data) {
                                mView.redPacketDetailSuccess(resp.data);
                            } else {
                                mView.redPacketDetailFailure(resp.msg);
                            }
                        } else {
                            mView.redPacketDetailFailure("获取红包详情失败，请稍后重试");
                        }
                    }
                });
    }

}
